package com.toocms.jinjuhang.ui.commodity.classify

import android.os.Bundle
import android.text.SpannableStringBuilder
import android.text.Spanned
import android.text.style.RelativeSizeSpan
import androidx.databinding.ObservableField
import com.blankj.utilcode.util.StringUtils
import com.toocms.jinjuhang.R
import com.toocms.jinjuhang.config.Constants
import com.toocms.jinjuhang.data.goods.CommodityItemData
import com.toocms.jinjuhang.ui.commodity.detils.CommodityDetailsFgt
import com.toocms.jinjuhang.ui.commodity.succour.SuccourDetailsFgt
import com.toocms.tab.base.MultiItemViewModel
import com.toocms.tab.binding.command.BindingAction
import com.toocms.tab.binding.command.BindingCommand

class ClassifyCommodityListGridItemModel(
    viewModel: ClassifyCommodityListModel, val item: ObservableField<CommodityItemData>
) : MultiItemViewModel<ClassifyCommodityListModel>(viewModel) {

    val itemClickBindingCommand = BindingCommand<BindingAction>(BindingAction {
        item.get()?.apply {
            when (viewModel.type) {
                Constants.Extra.PRECINCT_TYPE_COMMODITY -> {
                    val bundle = Bundle()
                    bundle.putString(Constants.BundleKey.KEY_GOODS_ID, goods_id)
                    viewModel.startFragment(CommodityDetailsFgt::class.java, bundle)
                }
                Constants.Extra.PRECINCT_TYPE_SALVATION -> {
                    val bundle = Bundle()
                    bundle.putString(Constants.BundleKey.KEY_REFUND_ID, rescue_id)
                    viewModel.startFragment(SuccourDetailsFgt::class.java,bundle)
                }
            }
        }
    })


    init {
        itemType = Constants.RecyclerViewItemType.ITEM_TYPE_ONE
    }

    fun sales(commodityType: String?, number: String?) = when (viewModel.type) {
        Constants.Extra.PRECINCT_TYPE_SALVATION -> { //救助
            val format = StringUtils.getString(R.string.str_sales_number_hint)
            String.format(format, number)
        }
        else -> { //普通商品
            when (commodityType) {
                Constants.Extra.COMMODITY_TYPE_INTEGRAL -> { //积分商品
                    val format = StringUtils.getString(R.string.str_change_number_hint)
                    String.format(format, number)
                }
                else -> {
                    val format = StringUtils.getString(R.string.str_sales_number_hint)
                    String.format(format, number)
                }
            }
        }
    }

    fun price(commodityType: String?, sum: String?) = when (viewModel.type) {
        Constants.Extra.PRECINCT_TYPE_SALVATION -> { //救助
            ""
        }
        else -> { //商品
            when (commodityType) {
                Constants.Extra.COMMODITY_TYPE_INTEGRAL -> {  //积分商品
                    val sum = if (sum.isNullOrEmpty()) "00" else sum
                    SpannableStringBuilder("$sum${StringUtils.getString(R.string.str_integral)}").apply {
                        setSpan(
                            RelativeSizeSpan(0.8f),
                            sum.length,
                            length,
                            Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
                        )
                    }
                }
                else -> { //普通商品，领取商品
                    val sum = if (sum.isNullOrEmpty()) "0.00" else sum
                    "${StringUtils.getString(R.string.str_money_symbol)}$sum"
                }
            }
        }
    }
}